<template>
  <div v-drag>
    <div class="mask" @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="loading">
      <div class="box_head">
        <p>设置用户授权使用额度</p>
        <i @click="closePopup(false)"></i>
      </div>
      <div class="box_body">
        <div class="line">
          <span class="lab"><i class="mark_icon">*</i>使用额度：</span>
          <div>
            <template>
              <el-input-number v-model.number="numbers" @change="handleChange" :min="0" label="描述文字"></el-input-number>
            </template>
          </div>
        </div>
        <p class="error_line">{{ infoTip }}</p>
      </div>
      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="confirmEdit">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
import {getDetail, setUserQuota} from "@/api/data/user/product";

export default {
  props: ['pId'],
  data() {
    return {
      numbers: 1,
      infoTip: '',
      tip_num: '',
      loading: false,
      userId: '',
      productId:''
    }
  },
  mounted() {
    this.getDetail();
  },
  methods: {
    getDetail() {
      let that = this;
      this.loading = true;
      getDetail(that.pId).then(res => {
        if (res) {
          that.setData(res);
        }
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    setData(obj) {
      this.numbers = obj.authNumber  ? obj.authNumber  : 0;
      this.productId = obj.productId ? obj.productId : '';
      this.userId = obj.userId ? obj.userId : '';
    },
    handleChange(val) {
      this.numbers = val;
    },

    // 确认编辑
    confirmEdit() {
      if (this.numbers === '') {
        this.setTip(1, '请输入使用额度！');
      } else if (this.numbers <= 0) {
        this.setTip(1, '使用额度需大于0！');
      } else if (this.numbers%1 !== 0) {
        this.setTip(1, '请输入整数！');
      }
      else {
        this.sendInfo();
      }
    },

    sendInfo() {
      let that = this;
      that.loading = true;
      let data = {
        productId: parseFloat(that.productId),
        userId: parseFloat(that.userId),
        authNumber: parseFloat(that.numbers),
      }
      setUserQuota(data).then(response => {
        that.closePopup(true);
        that.$message.success({
          message:'操作成功！',
          duration:1500
        });
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    setTip(num, text) {
      this.tip_num = num;
      this.infoTip = text
    },

    // 关闭
    closePopup(refresh) {
      this.$emit('listenToChild', {refresh: refresh})
    }
  }
}
</script>

<style scoped>
.half_box div {
  width: 124px !important;
}

.half_box .second_lab {
  width: 75px;
}
</style>
